package com.trade.module.base.controller;

import com.trade.module.base.entity.Resource;
import com.trade.module.base.entity.SystemLog;
import com.trade.module.base.service.ResourceService;
import com.trade.module.base.service.SystemLogService;
import com.trade.common.utils.*;
import com.trade.module.base.vo.ResourceVo;
import com.trade.module.base.vo.UserVo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Class Description: 资源管理控制器
 *
 * @Version v1.0 2017/4/26 0:14
 * @JDK version 1.8.0_51
 * @Author zfb
 */
@Log4j
@Controller
@RequestMapping(value = "base/resource")
public class ResourceController extends BaseController {
    /**
     * Field description：注入菜单资源服务层
     */
    @Autowired
    private ResourceService resourceService;

    /**
     * Field description：注入日志服务层
     */
    @Autowired
    private SystemLogService systemLogService;

    /**
     * Method description：新增菜单资源
     *
     * @param resource 资源对象
     * @return ResponseBean
     */
    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseBean save(@RequestBody Resource resource, HttpServletRequest request) {
        //获取当前操作人信息
        UserVo currentUser = getCurrentUser();
        String currentUserId = (currentUser != null) ? currentUser.getId() : "";
        //存储菜单资源
        resource.setModifyId(currentUserId);
        resource.setCreaterId(currentUserId);
        resource = resourceService.save(resource);
        //打印日志
        if (XaUtil.isNotEmpty(resource)) {
            SystemLog sysLog = LogUtil.buildeLogBean(Resource.class, LogUtil.LogLevel.info, LogUtil.CRUDType.create, LogUtil.OperatRsult.success
                    , currentUserId, (currentUser != null) ? currentUser.getAccount() : "", resource.getId(), NetUtil.getIp(request), "添加资源成功");
            systemLogService.save(sysLog);
            log.info(LogUtil.getLogJsonFromBean(sysLog).toString());

            // 设置返回信息
            return new ResponseBean(ResponseUtil.MODIFY_OK_201);
        } else {
            SystemLog sysLog = LogUtil.buildeLogBean(Resource.class, LogUtil.LogLevel.error, LogUtil.CRUDType.create, LogUtil.OperatRsult.failure
                    , currentUserId, (currentUser != null) ? currentUser.getAccount() : "", "", NetUtil.getIp(request), "添加资源失败");
            systemLogService.save(sysLog);
            log.error(LogUtil.getLogJsonFromBean(sysLog).toString());

            // 设置返回信息
            return new ResponseBean(ResponseUtil.VERIFY_ERROR_422);
        }
    }

    /**
     * Method description：根据条件分页查询字典数据
     *
     * @param pageNum            当前页码，为null或者为-1将查询所有有效数据
     * @param pageSize           每页查询条数，默认为10
     * @param sortJsonArray      排序的json数组，格式为[{property:"排序字段名",direction:"ASC/DESC"}]，null、空字符串及[]将被认为是不进行排序
     * @param conditionJsonArray 查询条件的json数组，格式为[{property:"字段名",option:"操作类型",value:"值",propertytype:"数据类型"}]
     *                           其中option的值有EQ(等于), IN(在值数组中), ISNULL(非空), LIKE(包含), GT(大于), LT(小于), GTE(大于等于),
     *                           LTE(小于等于), NE(不等于),LIKEIGNORE(包含，忽略大小写)
     *                           propertytype为DATETIME或DATE，DATETIME值格式为YYYY-MM-dd HH:mm:ss，DATE值格式为YYYY-MM-dd
     *                           <p/>
     *                           conditionJsonArray为null、空字符串及[]将被认为是不进行条件过滤
     * @return ResponseBean
     */
    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseBean<PageBean<ResourceVo>> getPage(@RequestParam(defaultValue = "-1") Integer pageNum,
                                                      @RequestParam(defaultValue = "10") Integer pageSize,
                                                      @RequestParam String sortJsonArray,
                                                      @RequestParam String conditionJsonArray, HttpServletRequest request) {

        //获取当前操作人信息
        UserVo currentUser = getCurrentUser();
        String currentUserId = (currentUser != null) ? currentUser.getId() : "";
        if (XaUtil.isNotEmpty(sortJsonArray)) {
            sortJsonArray = YjhhUtil.base64Decode(sortJsonArray);
        }
        if (XaUtil.isNotEmpty(conditionJsonArray)) {
            conditionJsonArray = YjhhUtil.base64Decode(conditionJsonArray);
        }
        PageBean<ResourceVo> pageBean = resourceService.findResourcePageByCondition(pageNum, pageSize, sortJsonArray, conditionJsonArray);
        SystemLog sysLog = LogUtil.buildeLogBean(Resource.class, LogUtil.LogLevel.info, LogUtil.CRUDType.read, LogUtil.OperatRsult.success
                , currentUserId, (currentUser != null) ? currentUser.getAccount() : "", "", NetUtil.getIp(request), "查询资源列表成功");
        systemLogService.save(sysLog);
        log.info(LogUtil.getLogJsonFromBean(sysLog).toString());
        return new ResponseBean<>(ResponseUtil.SELECT_OK_200, pageBean);
    }
}
